package com.kingdee.pda.ui.interception_registr;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.R;
import com.kingdee.pda.callback.OnCheckBoxChangeListener;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 拦截单登记
 * Created by dlj
 * DATE 2022/5/6
 */
public class InterceptionRegistrActivity extends BaseTitleActivity implements View.OnKeyListener, InterceptionRegistrContract.IOutCheckView {
    private EditText et_orderNo;
    private Button btn_confirm;
    private RecyclerView recyclerView;
    private InterceptionRegistrListAdapter adapter;
    private List<InterceptionOrderBean> lists = new ArrayList<>();

    private InterceptionRegistrPresenter presenter;
    private static String SCANORDER = "scan_mailNo";//扫描物流单的标识（扫描和拦截用的同一接口，所以用标识区分操作）
    private static String INTERCEPTIONORDER = "interception_billNo";//确定拦截出库单的标识

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, InterceptionRegistrActivity.class));
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_interception_registr);
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("拦截单登记");

        et_orderNo = findViewById(R.id.et_orderNo);
        setEditTextScannerEvents(et_orderNo, this);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new InterceptionRegistrListAdapter(this, lists, new OnCheckBoxChangeListener() {
            @Override
            public void onCheckChanged(int position, boolean isChecked) {
                if (isChecked) {
                    btn_confirm.setEnabled(true);
                }
            }
        });

        recyclerView.setAdapter(adapter);

        btn_confirm = findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int opinionPosition = adapter.getOpinionPosition();
                if (opinionPosition >= 0 && lists.size() > opinionPosition) {
                    InterceptionOrderBean interceptionOrderBean = lists.get(opinionPosition);
                    presenter.stockOutInterception(interceptionOrderBean.getMailNo(), interceptionOrderBean.getBillNo(), INTERCEPTIONORDER);
                }
            }
        });

        presenter = new InterceptionRegistrPresenter(this, this);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!(v instanceof EditText)) {
            return false;
        }
        EditText editText = (EditText) v;
        String input = editText.getText().toString().trim();
        if (TextUtils.isEmpty(input)) return false;
        if ((keyCode == KeyEvent.KEYCODE_SYSRQ || keyCode == KeyEvent.KEYCODE_ENTER) && event.getAction() == KeyEvent.ACTION_UP) {
            switch (v.getId()) {
                case R.id.et_orderNo:
                    if (!TextUtils.isEmpty(input)) {
                        presenter.stockOutInterception(input, "", SCANORDER);
                    }
                    break;
            }
        }
        return false;
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {

    }

    @Override
    public void stockOutInterceptionSuccess(List<InterceptionOrderBean> beans, String msg, String operation) {
        SpeakUtils.onlyRing(this, 2);
        CommonUtil.requestFocusAndClearText(et_orderNo);
        lists.clear();
        btn_confirm.setEnabled(false);

        //如果是扫描的物流单操作
        if (SCANORDER.equals(operation)) {
            lists.addAll(beans);
        }

        adapter.setOpinionPosition(-1);//设置adapter选中的下标为-1
        adapter.notifyDataSetChanged();
    }

    @Override
    public void stockOutInterceptionFailed(String msg) {
        CommonUtil.requestFocusAndClearText(et_orderNo);
        SpeakUtils.speakWithToast(InterceptionRegistrActivity.this, msg, true, false);
    }
}
